package com.ljw;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board3 extends JPanel implements ActionListener {

	private final int B_WIDTH 		=	350;
	private final int B_HEIGHT 		=	350;
	private final int INITIAL_X		=	-40;
	private final int INITIAL_Y		=	-40;
	private final int DELAY			=	25;
	
	private Image hahaha;
	private Timer timer;
	
	private int x, y;
	
	public Board3(){
		initBoard();
	}
	
	private void loadImage() {
		ImageIcon ii = new ImageIcon("./resource/img/hahaha.jpg");
		hahaha = ii.getImage();
	}
	
	private void initBoard(){
		setBackground(Color.black);
		setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
		setDoubleBuffered(true);
		
		loadImage();
		x = INITIAL_X;
		y = INITIAL_Y;
		
		timer = new Timer(DELAY, this);
		timer.start();
	}

	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		drawStar(g);
	}
	
	private void drawStar(Graphics g) {
		g.drawImage(hahaha, x, y, this);
		Toolkit.getDefaultToolkit().sync();
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		x += 1;
		y += 1;
		
		if(y > B_HEIGHT){
			y = INITIAL_Y;
			x = INITIAL_X;
		}	
		repaint();
	}
}
